package com.coursehub.exercise.repository;

import com.coursehub.exercise.entity.CourseQuestionBankQuestion;
import com.coursehub.exercise.entity.CourseQuestionBankQuestionId;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 课程题库题目关联数据访问层
 */
@Repository
public interface CourseQuestionBankQuestionRepository extends JpaRepository<CourseQuestionBankQuestion, CourseQuestionBankQuestionId> {
    
    /**
     * 根据课程题库ID查找所有题目关联
     * @param courseQuestionBankId 课程题库ID
     * @return 题目关联列表
     */
    List<CourseQuestionBankQuestion> findByIdCourseQuestionBankId(String courseQuestionBankId);
    
    /**
     * 根据题目ID查找所有课程题库关联
     * @param questionId 题目ID
     * @return 课程题库关联列表
     */
    List<CourseQuestionBankQuestion> findByIdQuestionId(String questionId);
    
    /**
     * 查询课程题库下的题目ID列表
     * @param courseQuestionBankId 课程题库ID
     * @return 题目ID列表
     */
    @Query("SELECT cq.id.questionId FROM CourseQuestionBankQuestion cq WHERE cq.id.courseQuestionBankId = :courseQuestionBankId")
    List<String> findQuestionIdsByCourseQuestionBankId(@Param("courseQuestionBankId") String courseQuestionBankId);
    
    /**
     * 检查课程题库和题目的关联是否存在
     * @param courseQuestionBankId 课程题库ID
     * @param questionId 题目ID
     * @return 是否存在关联
     */
    boolean existsByIdCourseQuestionBankIdAndIdQuestionId(String courseQuestionBankId, String questionId);
    
    /**
     * 删除课程题库和题目的关联
     * @param courseQuestionBankId 课程题库ID
     * @param questionId 题目ID
     */
    void deleteByIdCourseQuestionBankIdAndIdQuestionId(String courseQuestionBankId, String questionId);
    
    /**
     * 删除课程题库的所有题目关联
     * @param courseQuestionBankId 课程题库ID
     */
    void deleteByIdCourseQuestionBankId(String courseQuestionBankId);
    
    /**
     * 删除题目的所有课程题库关联
     * @param questionId 题目ID
     */
    void deleteByIdQuestionId(String questionId);
}
